﻿using Assessment.BLL;
using Assessment.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index_Gauge_GaugeRecordEdit : BasePage_Index
{
    #region 属性
    public int GaugeRecordId
    {
        get { return Request.QueryString["GaugeRecordId"] == null ? 0 : Convert.ToInt32(Request.QueryString["GaugeRecordId"]); }
    }

    public int GaugeRecordItemId
    {
        get { return Request.QueryString["GaugeRecordItemId"] == null ? 0 : Convert.ToInt32(Request.QueryString["GaugeRecordItemId"]); }
    }

    public int MedicalRecordId
    {
        get { return Request.QueryString["MedicalRecordId"] == null ? 0 : Convert.ToInt32(Request.QueryString["MedicalRecordId"]); }
    }

    #endregion

    UtilityBLL ubll = new UtilityBLL();
    T_GaugeRecordAnswerBLL answer_bll = new T_GaugeRecordAnswerBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            base.SetBtnGotoBackUrl("GaugeRecordView.aspx?MedicalRecordId=" + MedicalRecordId + "&GaugeRecordId=" + GaugeRecordId);
            BindRepeaterData();
        }
    }

    #region 数据绑定
    private void BindRepeaterData()
    {
        int rCount = 0;
        string swhere = " GaugeRecordItemId=" + GaugeRecordItemId + "";
        DataTable dt = ubll.GetListByPage("View_GaugeRecordAnswer", "*", "GaugeTestId", swhere, 10000, 1, 0, out rCount);
        rpt_List.DataSource = dt;
        rpt_List.DataBind();
    }
    #endregion

    #region 列表操作
    protected void RepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //{
        //    Label lbl_Valid = (Label)e.Item.FindControl("lbl_Valid");
        //    LinkButton lbtn_Valid = (LinkButton)e.Item.FindControl("lbtn_Valid");

        //    lbtn_Valid.Text = "修改";
        //    //设置启用禁用的显示文本
        //    if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Valid")) == 0)
        //    {
        //        lbl_Valid.Text = "禁用";
        //        lbl_Valid.ForeColor = System.Drawing.Color.Red;
        //    }
        //    else
        //    {
        //        lbl_Valid.Text = "启用";
        //        lbl_Valid.ForeColor = System.Drawing.Color.Green;
        //    }
        //}
    }
    protected void RepeaterList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //int id = int.Parse(e.CommandArgument.ToString());
        //if (e.CommandName == "Valid")
        //{
        //    try
        //    {
        //        Sys_Role model = new Sys_Role();
        //        model = bll.GetModel(id);
        //        model.Valid = model.Valid == 0 ? 1 : 0;
        //        bll.Update(model);
        //        BindRepeaterData(StrWhere);
        //    }
        //    catch (Exception ex)
        //    {
        //        base.Alert(ex.ToString());
        //    }

        //}
        //else if (e.CommandName == "Modify")
        //{
        //    Response.Redirect("UserEdit.aspx?Action=Modify&Id=" + id.ToString());
        //}
        //else if (e.CommandName == "Detail")
        //{
        //    Response.Redirect("UserEdit.aspx?Action=Detail&Id=" + id.ToString());
        //}
    }
    #endregion

    #region 保存按钮事件
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.rpt_List.Items)
        {
            Index_Gauge_GaugeTestItemList TestItemList = (Index_Gauge_GaugeTestItemList)item.FindControl("uc_GaugeTestItemList");
            T_GaugeRecordAnswer answer = new T_GaugeRecordAnswer();
            answer.GaugeRecordAnswerId = TestItemList.GaugeRecordAnswerId;

            answer.GaugeRecordItemId = GaugeRecordItemId;
            answer.GaugeTestId = TestItemList.GaugeTestId;
            answer.AnswerValue = TestItemList.AnswerValue;
            answer.TitleTexts = TestItemList.TitleTexts;
            
            if (answer.GaugeRecordAnswerId > 0)
            {
                answer_bll.Update(answer);
            }
            else
            {
                answer_bll.Add(answer);
            }

        }
        base.Redirect("操作成功!");
    }
    #endregion
}